package com.isoft.argi.services;

import com.isoft.argi.dao.UserDao;
import com.isoft.argi.pojo.User;
import com.isoft.argi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServices{

	@Autowired
	UserDao userDao ;

	public User userLogin(String name, String pwd) {
		if(StringUtil.isEmpty(name) || StringUtil.isEmpty(pwd)) {
			return  null ;
		}
		return userDao.userLogin(name , pwd) ;	}


	public int userRegister(String name, String pwd, String phone) {

		if(StringUtil.isEmpty(name) || StringUtil.isEmpty(pwd)) {
			return  -1;
		} else if(userDao.selectUserByName(name) == null){
			return userDao.userRegister(name , pwd , name + "@91isoft.com", phone) ;
		} else {
			return 0;
		}
	}

	public int userEdit(String id,String name, String pwd) {
		if(StringUtil.isEmpty(name) || StringUtil.isEmpty(pwd)) {
			return  -1 ;
		}
		return userDao.userEdit(id ,name , pwd) ;
	}

	public User selectUserByName(String name) {
		if (StringUtil.isEmpty(name)) {
			return null;
		} else {
			return userDao.selectUserByName(name);
		}
	}

	public int userCheck(String name, String phone) {
		if (StringUtil.isEmpty(name)) {
			return -1;
		} else if (userDao.selectUserByName(name) == null) {
			return 0;
		} else if (! (phone.equals(userDao.selectPhoneByName(name))) ) {
			return 2;
		} else {
			return 1;
		}
	}

	public int changePassword(String name, String password) {

		if(StringUtil.isEmpty(name) || StringUtil.isEmpty(password)) {
			return  -1 ;
		} else {
			return userDao.changeUserPassword(name, password);
		}

	}
}
